package cn.ffcs.bpmpvc.web;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.echoice.paper.PageBean;
import org.echoice.paper.PageNavi;

import cn.ffcs.bpmpvc.dao.DocTempleteDao;
import cn.ffcs.bpmpvc.model.PocDocTemplete;
import cn.ffcs.bpmpvc.services.bean.ConfigBean;
import cn.ffcs.bpmpvc.utils.FileUtil;
import cn.ffcs.util.context.ApplicationContextUtil;

import com.opensymphony.xwork2.ActionSupport;

public class AppDocTempleteAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private DocTempleteDao docTempleteDao;
	private List<File> uploads;
	private PocDocTemplete pocDocTemplete;
	private ConfigBean configBean;
	private String returnMsg;
	public String add(){
		return "toNew";
	}
	
	public String findList(){
		HttpServletRequest request=ApplicationContextUtil.getHttpServletRequest();
		PageBean pageBean=getDocTempleteDao().queryPageDocTempleteList(pocDocTemplete, PageNavUtil.getPageNo(request), PageNavUtil.getPageSize(request,30));
        request.setAttribute("docList", pageBean.getDataList());
        String pageNaviStr=PageNavi.getPageNaviText(request, pageBean);
        request.setAttribute("pageNaviStr", pageNaviStr);
		return "toList";
	}
	
	public String findDocList(){
		findList();
		return "toListToUser";
	}
	
	public String delete(){
		HttpServletRequest request=ApplicationContextUtil.getHttpServletRequest();
		String fileId=request.getParameter("fileId");
		getDocTempleteDao().remove(Integer.valueOf(fileId));
		return SUCCESS;
	}
	
	public String downFile() throws UnsupportedEncodingException{
		HttpServletRequest request=ApplicationContextUtil.getHttpServletRequest();
		String fileId=request.getParameter("fileId");
		PocDocTemplete docTemplete=getDocTempleteDao().get(Integer.valueOf(fileId));
		String fileName=configBean.getDocTemplatePath()+File.separator+docTemplete.getFileName();
		FileUtil.fileDown(fileName, ApplicationContextUtil.getHttpServletResponse());
		return null;
	}
	
	public String saveUpload(){
		File filePath=new File(configBean.getDocTemplatePath());
		if(!filePath.exists()){
			filePath.mkdirs();
		}
		String userName=ApplicationContextUtil.getUserContextObject().getCurrUser().getPerson().getName();
		if(uploads!=null&&uploads.size()>0){
			for (File file : uploads) {
				try {
					FileUtils.copyFileToDirectory(file, filePath);
					PocDocTemplete docTemplete=new PocDocTemplete();
					docTemplete.setFileName(file.getName());
					docTemplete.setCreater(userName);
					docTemplete.setCreateTime(new Date());
					getDocTempleteDao().save(docTemplete);
					FileUtils.forceDelete(file);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		this.returnMsg="模板上传成功！";
		return SUCCESS;
	}
	
	public DocTempleteDao getDocTempleteDao() {
		return docTempleteDao;
	}
	public void setDocTempleteDao(DocTempleteDao docTempleteDao) {
		this.docTempleteDao = docTempleteDao;
	}

	public List<File> getUploads() {
		return uploads;
	}

	public void setUploads(List<File> uploads) {
		this.uploads = uploads;
	}

	public PocDocTemplete getPocDocTemplete() {
		return pocDocTemplete;
	}

	public void setPocDocTemplete(PocDocTemplete pocDocTemplete) {
		this.pocDocTemplete = pocDocTemplete;
	}

	public ConfigBean getConfigBean() {
		return configBean;
	}

	public void setConfigBean(ConfigBean configBean) {
		this.configBean = configBean;
	}

	public String getReturnMsg() {
		return returnMsg;
	}

	public void setReturnMsg(String returnMsg) {
		this.returnMsg = returnMsg;
	}
	
}
